package com.zj.leetcode.medium.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: algorithm
 * @description: 路径总和 II
 * 113 path-sum-ii
 * @author: Zhang Bo
 * @create: 2022-06-23 14:54:19
 **/
public class PathSumIi {
    public static void main(String[] args) {
        Solution solution = new PathSumIi().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
            if(root == null) return new ArrayList<>();
            List<List<Integer>> ans = new ArrayList<>();
            if(root.val == targetSum && root.left == null && root.right == null){
                List<Integer> arr = new ArrayList<>();
                arr.add(root.val);
                ans.add(arr);
                return ans;
            }
            List<List<Integer>> left = pathSum(root.left,targetSum - root.val);
            List<List<Integer>> right = pathSum(root.right,targetSum - root.val);
            for(List<Integer> list : left){
                list.add(0,root.val);
                ans.add(list);
            }
            for(List<Integer> list : right){
                list.add(0,root.val);
                ans.add(list);
            }
            return ans;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
